package com.example.demo.repository.impl;

import com.example.demo.entity.Role;
import com.example.demo.entity.RolePerm;
import com.example.demo.entity1.Perm;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 测试sessionFactory
 * Created by Administrator on 2017/11/10.
 */
@Transactional
@Repository("TestImpl")
public class TestImpl {
    @Resource
    private SessionFactory sessionFactory;

    public Role getRoleByPK(long id){
        Session session = sessionFactory.getCurrentSession();
        return session.get(Role.class, id);
    }

    public List<Perm> queryPermByName(){
        String hql = "select p from com.example.demo.entity1.Perm p";
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery(hql);
        return query.list();
    }

    public List<RolePerm> queryPermBySQLName(){
        String sql = "SELECT R.ROLE_ID, P.PERM_ID, R.ROLE_NAME FROM PERM P, ROLE R WHERE P.PERM_ID = 1";
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createSQLQuery(sql).addEntity(RolePerm.class);
        return query.list();
    }
}
